Imports System.Data.SqlClient

Public Class Turnos : Implements IBusquedaFiltrado

    Private IdHora As Integer
    Private Hora As String

    Public Property IdHoraT() As Integer
        Get
            Return IdHora
        End Get
        Set(ByVal value As Integer)
            IdHora = value
        End Set
    End Property
    Public Property HoraT() As String
        Get
            Return Hora
        End Get
        Set(ByVal value As String)
            Hora = value
        End Set
    End Property
    Public Sub New()
        IdHora = 0
        Hora = ""
    End Sub
    Public Function Agregar(ByRef con As ConexionBD) As Boolean Implements IBusquedaFiltrado.Agregar

        Dim sql As String = ""
        Dim comando As SqlCommand = Nothing

        sql = String.Format("INSERT INTO TURNO (idhora,hora) VALUES({0},'{1}')", Me.IdHoraT, Me.HoraT)
        comando = New SqlCommand(sql, con.ConexionBase)
        If comando.ExecuteNonQuery() > 0 Then
            Return True
        Else
            Return True
        End If


    End Function
    
    Public Function Buscar(ByVal id As Integer, ByRef con As ConexionBD) As Boolean Implements IBusquedaFiltrado.Buscar

        Dim comando As SqlDataAdapter
        Dim rs As DataTableReader
        Dim dato As DataTable
        Dim sql As String
        Dim lResp As Boolean = False


        sql = String.Format("SELECT * FROM TURNOS WHERE IDHORA={0}", id)
        comando = New SqlDataAdapter(sql, con.ConexionBase)
        dato = New DataTable("turnos")
        comando.Fill(dato)
        rs = dato.CreateDataReader()
        comando.Dispose()
        dato.Dispose()

        If rs.Read() Then
            lResp = True
            Me.IdHoraT = System.Convert.ToInt16(rs("idHora"))
            Me.HoraT = rs("hora").ToString
        Else
            lResp = False
        End If
        rs.Close()

        Return lResp

    End Function

    Public Function Eliminar(ByVal id As Integer, ByRef con As ConexionBD) As Boolean Implements IBusquedaFiltrado.Eliminar
        Dim sql As String = ""
        Dim comando As SqlCommand = Nothing

        sql = String.Format("DELETE FROM TURNO WHERE IDHORA ={0}", id)
        comando = New SqlCommand(sql, con.ConexionBase)
        If comando.ExecuteNonQuery() > 0 Then
            Return True
        Else
            Return False
        End If
    End Function

    Public Function Modificar(ByRef con As ConexionBD) As Boolean Implements IBusquedaFiltrado.Modificar
        Dim sql As String = ""
        Dim comando As SqlCommand = Nothing

        sql = String.Format("UPDATE TURNO SET HORA = '{0}' WHERE IDHORA{1}", Me.HoraT, Me.IdHoraT)
        comando = New SqlCommand(sql, con.ConexionBase)
        If comando.ExecuteNonQuery() > 0 Then
            Return True
        Else
            Return False
        End If

    End Function
    'el nombre de esta funcion era CargarTurnos
    Public Function BuscarDatos(ByVal Fecha As String, ByVal mat As Integer, ByRef con As ConexionBD) As DataSet
        Dim datos As DataSet = New DataSet()
        Dim adapter As SqlDataAdapter
        Dim sql As String = ""

        sql = "select tup.contacto,tu.idhora,hora,(Nombre + ' ' + Apellido) as Paciente,pa.idtipodoc,pa.nrodoc" + _
             "     from turnos as tu left join turnosasignados as tup on tu.idhora = tup.idhora " + _
             "           left join paciente as pa on tup.idtipodoc = pa.idtipodoc and " + _
             "                                       tup.nrodoc = pa.nrodoc " + _
             "      where (fecha = '{0}'or fecha is null)  and (matricula = {1}or matricula is null)"
        sql = String.Format(sql, Fecha, mat)

        adapter = New SqlDataAdapter(sql, con.ConexionBase)
        adapter.Fill(datos, "turnosasignados")
        Return datos
    End Function
    
End Class
